import {ElMessageBox, ElMessageBoxOptions} from 'element-plus'
import {h} from 'vue'

/** elementUI公共方法 */

export type MessageBoxType='info'|'success' | 'warning' | 'info' | 'error'
export type MesageType='base'|'second'

export interface BaseMessageBoxConfig{
  title?:string //标题
  type?:MessageBoxType //类型
  hideCancel?:boolean //是否隐藏取消按钮
  confirmText?:string //确认按钮文字
  cancelText?:string //取消按钮文字
  mesageType:MesageType
}


export interface MessageBoxConfig extends BaseMessageBoxConfig{
  mesageType:'base'
}



export interface SecondMessageBoxConfig extends BaseMessageBoxConfig{
  mesageType:'second'
  secondMessage:string
}
/**
 * 消息弹出提示
 * @param message 信息
 * @param config 配置
 */
export const showMessageBox = (message:string, config?:MessageBoxConfig|SecondMessageBoxConfig):Promise<undefined> => {
  return new Promise((resolve, reject) => {
    let confirmText = '确认'
    let cancelText = '取消'
    let hideCancel = false
    let type:MessageBoxType = 'warning'
    let title = '删除确认'
    let secondMessage:string|undefined = undefined
    if (config) {
      if (config.confirmText) confirmText = config.confirmText
      if (config.cancelText) cancelText = config.cancelText
      if (config.hideCancel !== undefined) hideCancel = config.hideCancel
      if (config.type) type = config.type
      if (config.title) title = config.title
      if (config.mesageType === 'second') secondMessage = config.secondMessage
    }
    const elConfig:ElMessageBoxOptions = {
      confirmButtonText: confirmText,
      cancelButtonText:cancelText,
      type:type,
      showCancelButton:!hideCancel
    }
    ElMessageBox.confirm(
      !secondMessage ? message : h('div', null, [
        h('h2', null, message),
        h('h3', null, secondMessage)
      ]),
      title,
      elConfig
    ).then(() => {
      resolve(undefined)
    }).catch(() => {
      reject()
    })
  })
}